import {createStore,applyMiddleware} from 'redux'
import thunk from 'redux-thunk'
const initState={
   list:[1,2,3],
   city_list:[
       
   ], //城市列表
   ticket_list:[
    
   ], // 所有火车票列表
   search_ticket_list:[], //火车票搜索结果
}
let reducer=(state=initState,actions)=>{

   let newState=JSON.parse(JSON.stringify(state));
   switch(actions.type){
      case "SET_LIST":
          newState.list=actions.data;
          return newState;
       case "SET_CITY_LIST":
           newState.city_list=actions.data;
           return newState;
        case "SET_TICKET_LIST":
            newState.ticket_list=actions.data;
            return newState;
        case "SEARCH_TICKET":
            if(actions.data.from && actions.data.to){
                newState.search_ticket_list=newState.ticket_list.filter((item)=>{
                    return item.from===actions.data.from && item.to===actions.data.to
                })
            }else if(actions.data.from){
                newState.search_ticket_list=newState.ticket_list.filter((item)=>{
                    return item.from===actions.data.from 
                })
            }else if(actions.data.to){
                newState.search_ticket_list=newState.ticket_list.filter((item)=>{
                    return item.from===actions.data.from 
                })
            }else{
                newState.search_ticket_list=newState.ticket_list;
            }

          
           
            return newState;
    default:
        return state;
   }
}

export default createStore(reducer,applyMiddleware(thunk));